import { Component, OnInit, ViewChild, EventEmitter ,Input } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STColumn, STComponent, STPage, STChange } from '@delon/abc';
import { SFSchema,SFComponent, SFButton } from '@delon/form';
import { GridComponent, PopdetailComponent, HttpService, SupDic } from 'lbf';
import { NzMessageService, getRepeatedElement } from 'ng-zorro-antd';
import { RouterModule, Routes,Router } from '@angular/router';

@Component({
  selector: 'app-czy-qxxq',
  templateUrl: './qxxq.component.html',
})
export class CzyQxxqComponent implements OnInit {


  @Input()
  record:any;


  qxDataset=[];

  @ViewChild('qxst',{static:false}) qxst: STComponent;
  @ViewChild('qxAddPop',{static:false}) qxAddPop: PopdetailComponent;

  qxColumns: STColumn[] = [
   { title: '角色', index: 'RNAME'},
   { title: '机构编码', index: 'DEPTID'},
   { title: '部门名称', index: 'DEPTNAME'},
   {
     title: '操作区',
     buttons: [
       {
         text: '删除',
         click: (para: any) => this.delczy(para),
       },
     ],
   },
 ];

 deptlist=[];
 rolelist=[];
 dept_old='';

 qxSchema: SFSchema = {
   properties: {
     
   }
 };

     // st分页相关
     qxpage: STPage = {
      total: true,
      show: true,// 显示分页
      front: false // 关闭前端分页，true是前端分页，false后端控制分页
    };
    qxchange:STChange={
      type:"pi",
      pi:1,
      ps:10,
      total:0,
    }


  /************************************************************************************** */

   formData = {};

   constructor(
    private modal: ModalHelper,
    public msgSrv: NzMessageService,
    private lbservice: HttpService,
    private supdic: SupDic,
    private router:Router,
  ) { }

  ngOnInit() {
    this.lbservice.lbservice("CZY_getur",{'QXXY':this.record.QXXY,page:{'PAGESIZE':this.qxchange.ps,'PAGEIDX':1}}).then(resdata=>{
      this.qxDataset=resdata.message.list;
      this.qxchange.total=resdata.page.COUNT;
    });
    // 初始化部门列表
    this.lbservice.lbservice("qxgl_deptlist",{}).then(resdata=>{
      this.deptlist=resdata.message.list;
    });
   }


   /*********************************************************************************************************** */

  delczy(para:any){
    this.lbservice.lbservice("CZY_qxdelete",{'UID':para.USERID,'ROLEID':para.ROLEID}).then(resdata=>{
      this.qxchange.pi=1;
      this.stReload();
    });
  }

  stReload(){
    this.lbservice.lbservice("CZY_getur",{'QXXY':this.record.QXXY,page:{'PAGESIZE':this.qxchange.ps,'PAGEIDX':this.qxchange.pi}}).then(resdata=>{
      this.qxDataset=resdata.message.list;
      this.qxchange.total=resdata.page.COUNT;
    });
  }


    qxpageChange(para:any){
      if(para.type=="pi"){
        this.lbservice.lbservice('FR_qzblog',{'QXXY':this.record.QXXY,page:{'PAGESIZE':this.qxchange.ps,'PAGEIDX':para.pi}}).then(resdata=>{
          if(resdata.code==1){
            this.qxDataset=resdata.message.list;
            this.qxchange.pi=para.pi;
          }
       });
     }
    }
    //增加
    qxVisible=false;
    qxadd(){
      this.qxSchema={
        properties: {
          USERNAME:{
            type:'string',
            title:'用户名',
            default:this.record.USERID,
            readOnly:true,
          },
          USERID:{
            type:'string',
            title:'用户名',
            default:this.record.QXXY,
            ui:{
              hidden:true,
            }
          },
          DEPTID:{
            type:'string',
            title:'部门',
            enum:this.deptlist,
            ui:{
              widget:'tree-select',
              dropdownStyle:{
                height:'200px',
                overflow:'auto',
              },
            }
          },
          ROLEID:{
            type:'string',
            title:'角色',
            enum:this.rolelist,
            ui:{
              widget:'select',
            }
          },

        }
      };
      this.qxVisible=true;
      this.qxAddPop.isVisibleChange.subscribe(()=>{
        this.stReload();
      });
    }


    refresh(para:any){
      if(para.DEPTID!=this.dept_old){
        this.dept_old=para.DEPTID;
        this.lbservice.lbservice("qxgl_rolelist",{'DEPTID':para.DEPTID}).then(resdata=>{
          this.rolelist=resdata.message.list;
         this.rolelist.forEach(ele=>{
            ele.label=ele.LABEL;
            ele.value=ele.VALUE;
          });
          console.log(this.rolelist);
          this.reSchema(para);
        });
      }
    }


    reSchema(para:any){
      this.qxSchema={
        properties: {
          USERNAME:{
            type:'string',
            title:'用户名',
            default:this.record.USERID,
            readOnly:true,
          },
          USERID:{
            type:'string',
            title:'用户名',
            default:this.record.QXXY,
            ui:{
              hidden:true,
            }
          },
          DEPTID:{
            type:'string',
            title:'部门',
            default:para.DEPTID,
            enum:this.deptlist,
            ui:{
              widget:'tree-select',
              dropdownStyle:{
                height:'200px',
                overflow:'auto',
              },
            }
          },
          ROLEID:{
            type:'string',
            title:'角色',
            enum:this.rolelist,
            ui:{
              widget:'select',
            }
          },

        }
      };
    }
/*
   initDeptList(lt:any[]){
      lt.forEach(ele=>{
        ele.TITLE=ele.title;
        if(ele.disabled==0){
          ele.disabled=true;
        }if(ele.disabled==1){
          ele.disabled=true;
        }
      if(ele.children.length>0){
        this.initDeptList(ele.children);
      }
      });
    }*/
}
